/**
 * 
 */
package com.huangliusong;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import JDBCTools.MysqlUtils;

/**
 * @author huangliusong E-mail:huangliusong1994@gmail.com
 * @version 创建时间：2018年7月16日 下午3:28:08 类说明
 */
public class FileFrame {
	@SuppressWarnings("rawtypes")
	JList list;

	public static void main(String[] args) {
		// new QuartzScheduler().exeQuartz();
		new FileFrame().mainJframe();
	}

	public static void setSysClipboardText(String writeMe) {
		Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable tText = new StringSelection(writeMe);
		clip.setContents(tText, null);
	}

	@SuppressWarnings("rawtypes")
	public void mainJframe() {
		JFrame a = new JFrame("文件监控");// 顶层容器
		JMenuBar jmb = new JMenuBar();
		JMenu jMenu = new JMenu("同步文件");
		// 添加事件监听器(此处即为匿名类)
		jMenu.addMenuListener(new MenuListener() {

			@Override
			public void menuSelected(MenuEvent e) {
				// TODO Auto-generated method stub
				System.err.println("同步数据");
			}

			@Override
			public void menuDeselected(MenuEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void menuCanceled(MenuEvent e) {
				// TODO Auto-generated method stub

			}
		});
		list = new JList();
		DefaultListModel listModel = new DefaultListModel();
		List<Map<String, Object>> listDBNew = MysqlUtils.getData();
		for (int i = 0; i < listDBNew.size(); i++) {
			String filepath = listDBNew.get(i).get("filepath").toString();
			String createtime = listDBNew.get(i).get("createtime").toString();
			listModel.addElement(filepath + "------------------" + createtime);
		}
		list.setModel(listModel);
		test1Model(list);

		list.setBorder(BorderFactory.createTitledBorder("文件列表"));

		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				if (list.getSelectedIndex() != -1) {
					if (e.getClickCount() == 1) {
						String str = list.getSelectedValue().toString();
						String pathString = str.split("------------------")[0];
						setSysClipboardText(pathString);
					} else if (e.getClickCount() == 2) {
						String str = list.getSelectedValue().toString();
						String pathString = str.split("------------------")[0];
						try {
							java.awt.Desktop.getDesktop().open(
									new File(pathString));
						} catch (Exception e2) {
							System.err.println("文件不存在");
							JOptionPane.showMessageDialog(null,
									"" + e2.getMessage(), "监控提示",
									JOptionPane.ERROR_MESSAGE);
						}

					} else {
						System.err.println("其他操作");
						JOptionPane.showMessageDialog(null, "文件路径已经复制到剪切板");
					}
				}
			};
		});

		a.add(new JScrollPane());
		jmb.add(jMenu);

		a.setJMenuBar(jmb); // 添加菜单栏，不能设定位置，会自动放在最上部
		JScrollPane sp = new JScrollPane(list);
		a.getContentPane().add(sp);
		a.setBounds(260, 100, 488, 608);
		a.setVisible(true);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}

	@SuppressWarnings("rawtypes")
	public static void test1Model(JList jlist) {
		// 单位: 毫秒
		final long timeInterval = 1000;
		Runnable runnable = new Runnable() {
			public void run() {
				while (true) {
					List<Map<String, Object>> listDBNew = MysqlUtils.getData();
					List<Map<String, Object>> listDBOld = MysqlUtils
							.getDataOld();
					List<String> list = ReadDirectory.getAllFile("D:\\hlsXML",
							true);
					System.err.println(listDBNew.size() + listDBOld.size());
					System.err.println(list.size());
					if (listDBNew.size() + listDBOld.size() == list.size()) {
						System.err.println("数量对的上");
						DefaultListModel listModel1 = new DefaultListModel();
						List<Map<String, Object>> listDBNew1 = MysqlUtils
								.getData();
						for (int j = 0; j < listDBNew.size(); j++) {
							String filepath = listDBNew1.get(j)
									.get("filepath").toString();
							String createtime = listDBNew1.get(j)
									.get("createtime").toString();
							listModel1
									.addElement(filepath
											+ "------------------"
											+ createtime);
						}
						jlist.setModel(listModel1);
					} else {
						for (int i = 0; i < list.size(); i++) {
							File file = new File(list.get(i));
							long time = file.lastModified();// 返回文件最后修
															// 改时间，是以个long型毫秒数
							String ctime = new SimpleDateFormat(
									"yyyy-MM-dd hh:mm:ss")
									.format(new Date(time));
							String filePath = list.get(i);
							filePath = filePath.replace("\\", "\\\\");
							if (MysqlUtils.isData(filePath)) {
								MysqlUtils
										.update("insert into newfile(filepath,createtime) values('"
												+ filePath
												+ "','"
												+ ctime
												+ "')");
							}

						}
					}
					try {
						Thread.sleep(timeInterval);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		Thread thread = new Thread(runnable);
		thread.start();
	}
}
